<?php

namespace App\Traits\Relation;

use App\Models\Audit;
use App\Models\Department;
use App\Models\Consociation;
use App\Models\System\Module;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

trait CompanyRelation
{
	/**
	 * 获取公司已购买的模块
	 *
	 * Date: 22/03/2018
	 * @author George
	 * @return BelongsToMany
	 */
	public function modules()
	{
		return $this->belongsToMany(Module::class, 'company_module_pivot', 'company_id', 'module_code', 'id', 'code')->withTimestamps();
	}

	/**
	 * 获取公司部门信息
	 *
	 * Date: 18/04/2018
	 * @author George
	 * @return HasMany
	 */
	public function department()
	{
		return $this->belongsTo(Department::class, 'company_id', 'id');
	}

	/**
	 * 获取发起人信息
	 *
	 * Date: 24/04/2018
	 * @author George
	 * @return HasMany
	 */
	public function initiator()
    {
        return $this->hasMany(Consociation::class, 'initiator', 'id');
    }

	/**
	 * 获取合作公司ID
	 *
	 * Date: 24/04/2018
	 * @author George
	 * @return HasMany
	 */
    public function partner()
    {
        return $this->hasMany(Consociation::class, 'partner', 'id');
    }

	/**
	 * 获取审核记录
	 *
	 * Date: 24/04/2018
	 * @author George
	 * @return MorphOne
	 */
	public function audit()
	{
		return $this->morphOne(Audit::class, 'auditable');
	}
}
